<!-- 订单列表组件 -->
<template>
  <div>

    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区 -->
    <el-card>

      <!-- 搜索框区 -->
      <el-row>
        <el-col :span="8">
          <el-input placeholder="请输入内容">
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
      </el-row>

      <!-- 订单列表区 -->
      <el-table :data="orderlist" border stripe>

        <!-- 索引列 -->
        <el-table-column type="index" label="序号"></el-table-column>

        <!-- 订单编号列 -->
        <el-table-column label="订单编号" prop="order_number"></el-table-column>

        <!-- 订单价格列 -->
        <el-table-column label="订单价格" prop="order_price" width="80px"></el-table-column>

        <!-- 是否付款列 -->
        <el-table-column label="是否付款" width="100px">
          <template v-slot="scope">
            <el-tag type="danger" v-if="scope.row.pay_status === '0'">未付款</el-tag>
            <el-tag type="success" v-else>已付款</el-tag>
          </template>
        </el-table-column>

        <!-- 是否发货列 -->
        <el-table-column label="是否发货" prop="is_send" width="80px"></el-table-column>

        <!-- 下单时间列 -->
        <el-table-column label="下单时间" width="150px">
          <template v-slot="scope">
            {{ scope.row.create_time | dateFormat }}
          </template>
        </el-table-column>

        <!-- 操作列 -->
        <el-table-column label="操作" width="140px">
          <template v-slot="scope">
            <!-- {{scope.row.order_price}} -->
            <!-- 修改地址按钮 -->
            <el-tooltip effect="dark" content="修改地址" placement="top" :enterable="false">
              <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditAddressDialog"></el-button>
            </el-tooltip>
            <!-- 物流进度按钮 -->
            <el-tooltip effect="dark" content="物流进度" placement="top" :enterable="false">
              <el-button type="success" icon="el-icon-location" size="mini"
                @click="showProgressDialog('1106975712662', scope.row.order_number)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>

      </el-table>

      <!-- 页码/分页区 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum" :page-sizes="[5, 10, 15, 20]" :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper" :total="total" background>
      </el-pagination>

    </el-card>

    <!-- 修改地址对话框 -->
    <el-dialog title="修改地址" :visible.sync="editAddressDialogVisible" width="50%" @close="editAddressDialogClosed">
      <el-form :model="editAddressForm" :rules="editAddressFormRules" ref="editAddressFormRef" label-width="100px">
        <el-form-item label="省市区/县" prop="address1">
          <el-cascader :options="cityData" v-model="editAddressForm.address1" :props="{ expandTrigger: 'hover' }">
          </el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="editAddressForm.address2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editAddressDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editAddressDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 物流进度对话框 -->
    <el-dialog title="物流进度" :visible.sync="progressDialogVisible" width="50%">
      <el-row>
        <el-col>
          <p>订单编号：{{ currentOrderNumber }}</p>
        </el-col>
      </el-row>
      <!-- 时间线组件 -->
      <el-timeline :reverse="false">
        <el-timeline-item v-for="(activity, index) in progressInfo" :key="index" :timestamp="activity.time">
          {{activity.context}}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>

  </div>
</template>

<script>
// 导入中国城市数据(注意在 export default 外面)
import cityData from './citydata'

export default {
  data () {
    return {
      // 用于获取订单列表的请求数据
      queryInfo: {
        // 查询参数
        query: '',
        // 当前页码
        pagenum: 1,
        // 每页显示数据条数
        pagesize: 10
      },

      // 总的数据条数
      total: 0,

      // 获取到的订单列表数据
      orderlist: [],

      // 修改地址对话框显示与隐藏控制位
      editAddressDialogVisible: false,

      // 修改地址表单的数据
      editAddressForm: {
        // address因为是级联选择器，所以定义为数组
        address1: [],
        address2: ''
      },

      // 修改地址的表单验证规则
      editAddressFormRules: {
        address1: [
          { required: true, message: '请选择省市区/县', trigger: 'blur' }
        ],
        address2: [
          { required: true, message: '请输入详细地址', trigger: 'blur' }
        ]
      },

      // 中国城市数据(属性值与属性名同名可简写)
      // cityData: cityData
      cityData,

      // 物流进度对话框显示隐藏控制位
      progressDialogVisible: false,

      // 获取到的物流信息
      progressInfo: [],

      // 点击物流进度按钮时对应的订单编号
      currentOrderNumber: ''
    }
  },

  created () {
    this.getOrderList()
  },

  methods: {
    // 获取订单列表
    async getOrderList () {
      // 发起请求
      const { data: res } = await this.$http.get('orders', { params: this.queryInfo })
      if (res.meta.status !== 200) return this.$message.error('获取订单列表失败！')
      // console.log(res.data)
      this.total = res.data.total
      this.orderlist = res.data.goods
      // console.log(this.total, this.orderlist)
    },

    // 当前页变化时的处理函数
    handleCurrentChange (newPagenum) {
      // 把新的当前页赋值到请求中
      this.queryInfo.pagenum = newPagenum
      // 重新获取订单列表
      this.getOrderList()
    },

    // pagesize变化时的处理函数
    handleSizeChange (newPagesize) {
      // 赋值新的pagesize到请求中
      this.queryInfo.pagesize = newPagesize
      // 重新获取订单列表
      this.getOrderList()
    },

    // 展示修改地址对话框(编辑按钮处理函数)
    showEditAddressDialog () {
      this.editAddressDialogVisible = true
    },

    // 修改地址对话框关闭时的处理函数
    editAddressDialogClosed () {
      this.$refs.editAddressFormRef.resetFields()
    },

    // 展示物流进度对话框（物流进度按钮处理函数）
    async showProgressDialog (progressId, currentOrderNumber) {
      // 发起请求，获取物流信息
      const { data: res } = await this.$http.get('/kuaidi/' + progressId)
      if (res.meta.status !== 200) return this.$message.error('获取物流信息失败！')
      this.progressInfo = res.data
      this.currentOrderNumber = currentOrderNumber
      // console.log(this.progressInfo)
      // console.log(this.currentOrderNumber)
      this.progressDialogVisible = true
    }
  }

}
</script>

<style lang="less" scoped>
// 级联选择器样式
.el-cascader {
  width: 100%;
}
// // 旧版本引入时间线组件的样式
// @import '../../plugins/timeline/timeline.css'
// @import '../../plugins/timeline-item/timeline-item.css'
</style>
